package com.ysd.crm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.crm.dao.IRolesDao;
import com.ysd.crm.dao.RoleModuleDao;
import com.ysd.crm.model.RoleModule;
import com.ysd.crm.model.Roles;

@RestController
@RequestMapping("/roles")
public class RolesController {

	@Resource
	private IRolesDao rolesDao;
	@Resource
	private RoleModuleDao roleModuleDao;
	
	@RequestMapping("findAll")
	public Map findAll() {
		List<Roles> list = rolesDao.findAll();
		Map map = new HashMap();
		map.put("code", 0);
		map.put("count", 100);
		map.put("data", list);
		return map;
	}
	
	@RequestMapping("save")
	public void save(Roles role) {
		rolesDao.save(role);
	}
	
	@RequestMapping("delOne")
	public void delOne(int id) {
		rolesDao.deleteById(id);
	}
	
	//分配权限
	@RequestMapping("setModule")
	public void setModule(String ids,Integer rid) {
		System.out.println("rid:"+rid);
		//分配模块前先清空该角色模块
		roleModuleDao.delByRid(rid);
		
		String idArr[] = ids.split(",");
		for(String id:idArr) {
			if(id.length()>0) {
				RoleModule rm = new RoleModule();
				rm.setRid(rid);
				rm.setMid(Integer.parseInt(id));
				roleModuleDao.save(rm);
			}
		}
	}

}
